/* Project: POPS
 * CS 279, Vanderbilt University
 * by: Igor I. Ges, Michael S. Parrish, Mark E. Sakauye
 *
 *  This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.popsGUI.java;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.swing.table.DefaultTableModel;

import com.popsDB.java.dto.Items;

//import com.popsDB.java.dao.CustomersDao;
//import com.popsDB.java.dao.ItemsDao;
//import com.popsDB.java.dao.OrdereditemsDao;
//import com.popsDB.java.dao.OrdersDao;
//import com.popsDB.java.factory.CustomersDaoFactory;
//import com.popsDB.java.factory.ItemsDaoFactory;
//import com.popsDB.java.factory.OrdereditemsDaoFactory;
//import com.popsDB.java.factory.OrdersDaoFactory;

/**
 *
 * @author  __USER__
 */
public class EditMasterPriceSheet extends javax.swing.JPanel {

	/** Creates new form EditMasterPriceSheet */
	public EditMasterPriceSheet() {
		initComponents();
		notificationDialog = new com.popsGUI.java.notificationDialog(null,false);
	}

	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		confirmation_dialog = new javax.swing.JDialog();
		c_label1 = new javax.swing.JLabel();
		c_label2 = new javax.swing.JLabel();
		c_yes_button = new javax.swing.JButton();
		c_no_button = new javax.swing.JButton();
		jScrollPane8 = new javax.swing.JScrollPane();
		empsp_itemList_table = new javax.swing.JTable();
		empsp_saveChanges_button = new javax.swing.JButton();
		empsp_cancel_button = new javax.swing.JButton();

		confirmation_dialog.setMinimumSize(new java.awt.Dimension(328, 200));

		c_label1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		c_label1.setText("Any information you entered will not be saved.");

		c_label2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		c_label2.setText("Do you still want to cancel?");

		c_yes_button.setText("Yes");
		c_yes_button.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				c_yes_buttonActionPerformed(evt);
			}
		});

		c_no_button.setText("No");
		c_no_button.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				c_no_buttonActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout confirmation_dialogLayout = new javax.swing.GroupLayout(
				confirmation_dialog.getContentPane());
		confirmation_dialog.getContentPane().setLayout(
				confirmation_dialogLayout);
		confirmation_dialogLayout
				.setHorizontalGroup(confirmation_dialogLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								confirmation_dialogLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												confirmation_dialogLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																c_label2,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																304,
																Short.MAX_VALUE)
														.addComponent(
																c_label1,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																304,
																Short.MAX_VALUE)
														.addGroup(
																javax.swing.GroupLayout.Alignment.TRAILING,
																confirmation_dialogLayout
																		.createSequentialGroup()
																		.addComponent(
																				c_yes_button,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				64,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																				176,
																				Short.MAX_VALUE)
																		.addComponent(
																				c_no_button)))
										.addContainerGap()));

		confirmation_dialogLayout.linkSize(
				javax.swing.SwingConstants.HORIZONTAL,
				new java.awt.Component[] { c_no_button, c_yes_button });

		confirmation_dialogLayout
				.setVerticalGroup(confirmation_dialogLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								confirmation_dialogLayout
										.createSequentialGroup()
										.addGap(31, 31, 31)
										.addComponent(
												c_label1,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												53,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(c_label2)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED,
												55, Short.MAX_VALUE)
										.addGroup(
												confirmation_dialogLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																c_no_button)
														.addComponent(
																c_yes_button,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																25,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addContainerGap()));

		setPreferredSize(new java.awt.Dimension(1024, 679));

		empsp_itemList_table.setModel(new javax.swing.table.DefaultTableModel(
				new Object[][] { { null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null } },
				new String[] { "ID", "Name", "Description", "1st Unit",
						"Price 1", "2nd Unit", "Price 2" }) {
			Class[] types = new Class[] { java.lang.Integer.class,
					java.lang.String.class, java.lang.String.class,
					java.lang.String.class, java.lang.Float.class,
					java.lang.String.class, java.lang.Float.class };
			boolean[] canEdit = new boolean[] { false, false, false, false,
					true, false, true };

			public Class getColumnClass(int columnIndex) {
				return types[columnIndex];
			}

			public boolean isCellEditable(int rowIndex, int columnIndex) {
				return canEdit[columnIndex];
			}
		});
		empsp_itemList_table.setToolTipText("");
		jScrollPane8.setViewportView(empsp_itemList_table);

		empsp_saveChanges_button.setText("Save Changes");
		empsp_saveChanges_button
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						empsp_saveChanges_buttonActionPerformed(evt);
					}
				});

		empsp_cancel_button.setText("Cancel");
		empsp_cancel_button
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						empsp_cancel_buttonActionPerformed(evt);
					}
				});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addComponent(
																				jScrollPane8,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				1000,
																				Short.MAX_VALUE)
																		.addContainerGap())
														.addGroup(
																javax.swing.GroupLayout.Alignment.TRAILING,
																layout
																		.createSequentialGroup()
																		.addComponent(
																				empsp_saveChanges_button,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				141,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addGap(
																				28,
																				28,
																				28)
																		.addComponent(
																				empsp_cancel_button)
																		.addGap(
																				354,
																				354,
																				354)))));

		layout.linkSize(javax.swing.SwingConstants.HORIZONTAL,
				new java.awt.Component[] { empsp_cancel_button,
						empsp_saveChanges_button });

		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup().addGap(42, 42, 42).addComponent(
						jScrollPane8, javax.swing.GroupLayout.PREFERRED_SIZE,
						437, javax.swing.GroupLayout.PREFERRED_SIZE).addGap(18,
						18, 18).addGroup(
						layout.createParallelGroup(
								javax.swing.GroupLayout.Alignment.BASELINE)
								.addComponent(empsp_saveChanges_button,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										52,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addComponent(empsp_cancel_button))
						.addContainerGap(130, Short.MAX_VALUE)));

		layout.linkSize(javax.swing.SwingConstants.VERTICAL,
				new java.awt.Component[] { empsp_cancel_button,
						empsp_saveChanges_button });

	}// </editor-fold>
	//GEN-END:initComponents

	private void c_no_buttonActionPerformed(java.awt.event.ActionEvent evt) {
		confirmation_dialog.setVisible(false);
	}

	private void c_yes_buttonActionPerformed(java.awt.event.ActionEvent evt) {
		this.fillOutEMPSPTable();
		confirmation_dialog.setVisible(false);
	}

	private void empsp_cancel_buttonActionPerformed(
			java.awt.event.ActionEvent evt) {
		confirmation_dialog.setVisible(true);
	}

	private void empsp_saveChanges_buttonActionPerformed(
			java.awt.event.ActionEvent evt) {
		this.saveChanges();
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JLabel c_label1;
	private javax.swing.JLabel c_label2;
	private javax.swing.JButton c_no_button;
	private javax.swing.JButton c_yes_button;
	private javax.swing.JDialog confirmation_dialog;
	private javax.swing.JButton empsp_cancel_button;
	private javax.swing.JTable empsp_itemList_table;
	private javax.swing.JButton empsp_saveChanges_button;
	private javax.swing.JScrollPane jScrollPane8;
	// End of variables declaration//GEN-END:variables
	private com.popsGUI.java.notificationDialog notificationDialog;
	//TODO: the start of our code
	////////////////Our variable declarations /////////////////
	//CustomersDao CustomersDAO = CustomersDaoFactory.create();
	//ItemsDao ItemsDAO = ItemsDaoFactory.create();
	//OrdereditemsDao OrdereditemsDAO = OrdereditemsDaoFactory.create();
	//OrdersDao OrdersDAO = OrdersDaoFactory.create();

	Mediator MEDIATOR = Mediator.getInstance();

	DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
	DecimalFormat decimalPriceFormat = new DecimalFormat("###,###,##0.00");

	final private int EMPSP_ID_COL = 0;
	final private int EMPSP_NAME_COL = 1;
	final private int EMPSP_DESCRIPTION_COL = 2;
	final private int EMPSP_UNIT1_COL = 3;
	final private int EMPSP_PRICE1_COL = 4;
	final private int EMPSP_UNIT2_COL = 5;
	final private int EMPSP_PRICE2_COL = 6;

	///////////// End of our variable declarations /////////////

	// Fill out the table with all relevant information
	public void fillOutEMPSPTable() {
		ArrayList<Items> items_list = MEDIATOR.getItemsList();

		// If the default table isn't big enough to fit all the items,
		// add the correct number of rows to fit them
		if (empsp_itemList_table.getModel().getRowCount() < items_list.size()) {
			this.addRowsToTable(items_list.size()
					- empsp_itemList_table.getModel().getRowCount());
		}

		// fill the table with info from the items_list
		for (int row = 0; row < items_list.size(); row++) {
			this.empsp_itemList_table.setValueAt(items_list.get(row)
					.getItemID(), row, EMPSP_ID_COL);
			this.empsp_itemList_table.setValueAt(items_list.get(row)
					.getItemName(), row, EMPSP_NAME_COL);
			this.empsp_itemList_table.setValueAt(items_list.get(row)
					.getItemDescription(), row, EMPSP_DESCRIPTION_COL);
			this.empsp_itemList_table.setValueAt(items_list.get(row)
					.getUnitName(), row, EMPSP_UNIT1_COL);
			this.empsp_itemList_table.setValueAt(items_list.get(row)
					.getUnitPrice(), row, EMPSP_PRICE1_COL);
			this.empsp_itemList_table.setValueAt(items_list.get(row)
					.getOtherUnitName(), row, EMPSP_UNIT2_COL);
			this.empsp_itemList_table.setValueAt(items_list.get(row)
					.getOtherUnitPrice(), row, EMPSP_PRICE2_COL);

		}
	}

	// Adds numToAdd rows to the table
	private void addRowsToTable(int numToAdd) {
		//can't add < 1 rows, so we only do stuff if it's a viable numbers
		if (numToAdd > 0) {
			DefaultTableModel curModel = (DefaultTableModel) empsp_itemList_table
					.getModel();

			for (int i = 0; i < numToAdd; i++) {
				// Default values for each new row
				curModel.addRow(new Object[] { null, null, null, null, null,
						null, null });
			}
			empsp_itemList_table.setModel(curModel);
		}
	}

	private void saveChanges() {
		////////////////////
		//copy DB to array//
		////////////////////
		ArrayList<Items> _itmsArr = new ArrayList<Items>();

		int row = 0;
		boolean noNegatives = true;

		//go through all the table
		while (this.empsp_itemList_table.getValueAt(row, EMPSP_ID_COL) != null
				&& noNegatives) {
			/////////////////////////////////
			//put item info into item object/
			/////////////////////////////////
			Float price1 = (Float) empsp_itemList_table.getValueAt(row,
					EMPSP_PRICE1_COL);
			Float price2 = (Float) empsp_itemList_table.getValueAt(row,
					EMPSP_PRICE2_COL);

			if (price1 > 0 && price2 > 0) {
				Items _item = new Items();
				_item.setItemID((Integer) this.empsp_itemList_table.getValueAt(
						row, EMPSP_ID_COL));
				_item.setItemName((String) this.empsp_itemList_table
						.getValueAt(row, EMPSP_NAME_COL));
				_item.setItemDescription((String) this.empsp_itemList_table
						.getValueAt(row, EMPSP_DESCRIPTION_COL));
				_item.setUnitName((String) this.empsp_itemList_table
						.getValueAt(row, EMPSP_UNIT1_COL));
				_item.setUnitPrice((Float) this.empsp_itemList_table
						.getValueAt(row, EMPSP_PRICE1_COL));
				_item.setOtherUnitName((String) this.empsp_itemList_table
						.getValueAt(row, EMPSP_UNIT2_COL));
				_item.setOtherUnitPrice((Float) this.empsp_itemList_table
						.getValueAt(row, EMPSP_PRICE2_COL));

				_itmsArr.add(_item);
			} else {
				noNegatives = false;
			}

			row++;
		}

		/////////////////
		//pass array to// 
		/////////////////
		String infoText = "<html><p align=center>";
//		error_dialog.setVisible(true); bug#102

		if (noNegatives) {
			if (MEDIATOR.saveItemsFromMasterPS(_itmsArr)) {
				infoText += "Saved successfully!<p><p>";
				infoText += "Table reloaded to DB values</html>";
				notificationDialog.displayNotification(infoText);
				this.fillOutEMPSPTable();
			} else {
				infoText += "Save failed.<p><p>";
				infoText += "Please retry.</html>";
				notificationDialog.displayNotification(infoText);
			}
		} else {
			infoText += "Negative and zero values for prices aren't allowed!<p><p>";
			infoText += "Change the incorrect values then try again.</html>";
			notificationDialog.displayNotification(infoText);
		}
	}

	public void setEditMasterPriceSheetComponentsEnabledParameterTo(
			boolean enable) {
		empsp_saveChanges_button.setEnabled(enable);
		empsp_cancel_button.setEnabled(enable);
	}
}